
var newsRules = {

	'#allowComments': function( element ) {
		element.onclick = function() {
			if( element.checked ) {
				new Effect.BlindDown( 'commentChallengeContainer',
					{duration:.2}
				);
			} else {
				new Effect.BlindUp( 'commentChallengeContainer',
					{duration:.2}
				);

			}
		}
	},

	'.newsPublishPost': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id && $('infoBumper'+id).style.display == 'none' ) {
				var span = document.createElement( 'span' );
				span.className = 'infoBumper';
				
				new Insertion.Top( span, 'Are you sure you want to publish this post?' );
				
				var button = document.createElement( 'input' );
				button.type = 'button';
				button.value = 'Publish';
				button.className = 'newsPublishNow';
				button.setAttribute( 'for', id );
				button.style.marginLeft = '1em';
				button.style.marginRight = '1em';
				
				var href = document.createElement( 'a' );
				href.href = '#';
				href.innerHTML = 'Cancel';
				href.className = 'newsPublishCancel';
				href.id = 'newsPublishCancel'+id;
				
				span.appendChild( button );
				span.appendChild( href );
				
				$('infoBumper'+id).appendChild( span );

				Behaviour.apply();

				new Effect.BlindDown( 'infoBumper'+id, {duration: .2} );
			}
		}
	},
	
	'.newsPublishCancel': function( element ) {
		element.onclick = function() {
			new Effect.BlindUp( element.parentNode.parentNode, {duration: .2} );
			Element.remove( element.parentNode );
		}
	},
	
	'.newsPublishNow': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			
			if( id ) {
	
				var spinner = document.createElement( 'img' );
				spinner.id = 'articleEditSpinner';
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				$('infoBumper'+id).appendChild( spinner );
				
				var url = BASEURL+'/'+MKEY+'/publish';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id, evalScripts: true, method: 'post'}
				);
				
				window.publish = function( data ) {

					if( data.saved ) {
					
						Element.remove( spinner );
						$('newsPublishCancel'+id).onclick();
					
						$('newsPublishPostStatus'+id).src =
							$('newsPublishPostStatus'+id).src.replace(
								new RegExp( 'star_inactive.png' ),
								'star.png'
							);
							
						actionMessage( 'Post '+id+' Published!' );
					}

				}
			}			
			

		}
	},

	'.newsDeletePost': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id && $('infoBumper'+id).style.display == 'none' ) {
				var span = document.createElement( 'span' );
				span.className = 'infoBumper';
				
				new Insertion.Top( span, 'Are you sure you want to <b>delete</b> this post?' );
				
				var button = document.createElement( 'input' );
				button.type = 'button';
				button.value = 'Delete';
				button.className = 'newsDeleteNow';
				button.setAttribute( 'for', id );
				button.style.marginLeft = '1em';
				button.style.marginRight = '1em';
				
				var href = document.createElement( 'a' );
				href.href = '#';
				href.innerHTML = 'Cancel';
				href.className = 'newsPublishCancel';
				href.id = 'newsPublishCancel'+id;
				
				span.appendChild( button );
				span.appendChild( href );
				
				$('infoBumper'+id).appendChild( span );

				Behaviour.apply();

				new Effect.BlindDown( 'infoBumper'+id, {duration: .2} );
			}
		}
	},
	
	'.newsDeleteNow': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			
			if( id ) {
	
				var spinner = document.createElement( 'img' );
				spinner.id = 'articleEditSpinner';
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				$('infoBumper'+id).appendChild( spinner );
				
				var url = BASEURL+'/'+MKEY+'/deletePost';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id, evalScripts: true, method: 'post'}
				);
				
				window.deletePost = function( data ) {

					if( data.saved ) {
					
						Element.remove( spinner );
						$('newsPublishCancel'+id).onclick();
											
						new Effect.Puff( 'postContainer'+id );
						actionMessage( 'Post '+id+' deleted!' );
					}

				}
			}			
			

		}
	}
};

Behaviour.register( newsRules );